<?php
/* / / / / / ENGINE  ROOM / / / / / / /
|	          FRESH MOJO              |
|       (aka. SESSION handler)        |
|                                     |
| version:     0.0.1                  |
| last edit:   20080410               |
| package:     beta.r.0.0             |
/ / / / / / / / / / / / / / / / / / /*/

// Fresh or getting smelly?
unset( $_SESSION['init'] );

function GetLanguage(){
	$lang=(isset($_SERVER["HTTP_ACCEPT_LANGUAGE"]))? substr(strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"]),0,2) : 'en';
	return ( strpos($_SESSION['init']['langs'],$lang) === false)? $_SESSION['init']['langmaster'] : $lang;
}


// CACHE CONTROL
if( $response = $DB->query( "select `key`, value from settings where `key` in ( '".implode( "','", $settings_global )."' )" ))	
	while( $row = mysql_fetch_array( $response, MYSQL_ASSOC )) $_SESSION['init'][ $row[ 'key' ]] = $row[ 'value' ];
	
$_SESSION['init']['langmaster']	= array_shift(explode(",", $_SESSION['init']['langs']));
$_SESSION['init']['dictionary']	= $Modcon->request_json( engine_get( "lang.dictionary" ));
$_SESSION['init']['account']	= ACCOUNT;

// GETVAR MANIPULATIONS
if(isset($_GET['fresh'])) $fresh = explode(':', $_GET['fresh']);

if($fresh[0] == "lang") $_SESSION['init']['lang'] = $fresh[1];
else $_SESSION['init']['lang'] = isset($_COOKIE['lang'])? $_COOKIE['lang'] : GetLanguage();

$theme_array = explode(",",$_SESSION['init']['theme']);
if($fresh[0] == "theme") $_SESSION['init']['theme'] = $fresh[1];
else if(isset($_COOKIE['theme'])) $_SESSION['init']['theme'] = $_COOKIE['theme'];
if(!in_array($_SESSION['init']['theme'], $theme_array)) $_SESSION['init']['theme'] = $theme_array[0];

setcookie('lang', $_SESSION['init']['lang'], time()+9990000);

// SITEMAP CONTROL
define("LANG", $_SESSION['init']['lang'], true);


$_SESSION['map'] = array (
			   
	"menu"	=> $DB->filter_key( "page", "!parent && !private && active order by hierarchy", "ID, name" ),
	"num"	=> $DB->filter_key( "page", "active order by ID", array("ID", "name, active, mods, parent, private, meta, nameid, textid, <%lang:pages.nameid%> as title, (select group_concat(sp.ID order by sp.hierarchy) from pages as sp where sp.parent=pages.ID && sp.active group by sp.parent ) as sub" )),
	"str"	=> $DB->filter_key( "page", "active", array("name", "ID" ))
);

/*
$_SESSION['map'] = array (
			   
	"menu"	=> $DB->filter_key( "page", "!parent AND active ORDER BY parent ASC, hierarchy ASC", "*, <%lang:pages.nameid%> as title, (select group_concat(sp.ID order by sp.hierarchy) from pages as sp where sp.parent=pages.ID group by sp.parent ) as sub" ),
	"num"	=> $DB->filter_key( "page", "active", array("ID", "name", "parent" )),
	"str"	=> $DB->filter_key( "page", "active", array("name", "ID", "parent" ))
);
*/

?>